//
//  AppInfo.swift
//  Exchange
//
//  Created by rui on 2018/6/5.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class AppInfo:NSObject, NSCoding {
    
    var isLogin: Bool = false {
        didSet {
            self.changeLoginState()
        }
    }
    var nickName: String?
    var loginName: String?
    var email: String?
    var token: String? 
    var mobile: String?
    var portrait: String?
    var userId: Int?
    var reviews: String?
    var trustNum: Int?
    var expirationDate:Int64?
    var account:String = ""

    static let `default`: AppInfo = AppInfo()
    
    override init() {
        super.init()
        NotificationCenter.default.addObserver(self, selector: #selector(write), name: .UIApplicationDidEnterBackground, object: nil)
        guard let data = AppCacheInfo.default.object(forKey: "user") as? Data  else {
            return
        }
        guard let obj = NSKeyedUnarchiver.unarchiveObject(with: data) as? AppInfo else { return }
        self.loginName = obj.loginName
        self.nickName = obj.nickName
        self.email = obj.email
        self.token = obj.token
        self.mobile = obj.mobile
        self.portrait = obj.portrait
        self.userId = obj.userId
        self.reviews = obj.reviews
        self.trustNum = obj.trustNum
        self.expirationDate = obj.expirationDate
        if let token = token, !token.isEmpty {
            self.isLogin = true
        }else {
            self.isLogin = false
        }
        self.account = obj.account
    }
    
    func install(data: [String:Any]) {
        if let loginName = data["loginName"] as? String {
            self.loginName = loginName
        }
        if let email = data["email"] as? String {
            self.email = email
        }
        if let token = data["token"] as? String {
            self.token = token
        }
        if let nickName = data["nickName"] as? String {
            self.nickName = nickName
        }
        if let portrait = data["portrait"] as? String {
            self.portrait = portrait
        }
        if let userId = data["userId"] as? Int {
            self.userId = userId
        }
        if let trustNum = data["trustNum"] as? Int {
            self.trustNum = trustNum
        }
        if let reviews = data["reviews"] as? String {
            self.reviews = reviews
        }
        if let expirationDate = data["expirationDate"] as? Int64 {
            self.expirationDate = expirationDate
        }
        if let token = token, !token.isEmpty {
            self.isLogin = true
        }else {
            self.isLogin = false
        }
        write()
    }
    
    @objc func write() {
        let data = NSKeyedArchiver.archivedData(withRootObject: self)
        AppCacheInfo.default.set(data, forKey: "user")
    }
    
    
   
    
    private func changeLoginState() {
        if !self.isLogin {
            self.token = nil
            self.nickName = nil
            self.loginName = nil
            self.email = nil
            self.mobile = nil
            self.portrait = nil
            self.userId = nil
            self.reviews = nil
            self.trustNum = nil
            self.expirationDate = nil
            write()
            AppSecurityInfo.default.model = SecurityModel()
            AppSecurityInfo.default.openScreenLock(dic: [:])
        }
        NotificationCenter.default.post(name: NSNotification.Name.Exchange.loginDidChange, object: nil)
        NotificationCenter.default.post(name: NSNotification.Name.Exchange.adListNeedRefresh, object: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        if let nickName = aDecoder.decodeObject(forKey: "nickName") as? String {
            self.nickName = nickName
        }
        if let loginName = aDecoder.decodeObject(forKey: "loginName") as? String {
            self.loginName = loginName
        }
        if let email = aDecoder.decodeObject(forKey: "email") as? String {
            self.email = email
        }
        if let token = aDecoder.decodeObject(forKey: "token") as? String {
            self.token = token
        }
        if let mobile = aDecoder.decodeObject(forKey: "mobile") as? String {
            self.mobile = mobile
        }
        if let portrait = aDecoder.decodeObject(forKey: "portrait") as? String {
            self.portrait = portrait
        }
        if let userId = aDecoder.decodeObject(forKey: "userId") as? Int {
            self.userId = userId
        }
        if let reviews = aDecoder.decodeObject(forKey: "reviews") as? String {
            self.reviews = reviews
        }
        if let trustNum = aDecoder.decodeObject(forKey: "trustNum") as? Int {
            self.trustNum = trustNum
        }
        if let expirationDate = aDecoder.decodeObject(forKey: "expirationDate") as? Int64 {
            self.expirationDate = expirationDate
        }
        
        if let account = aDecoder.decodeObject(forKey: "account") as? String {
            self.account = account
        }
    }
    
    func encode(with aCoder: NSCoder) {
        aCoder.encode(nickName, forKey: "nickName")
        aCoder.encode(loginName, forKey: "loginName")
        aCoder.encode(email, forKey: "email")
        aCoder.encode(token, forKey: "token")
        aCoder.encode(mobile, forKey: "mobile")
        aCoder.encode(portrait, forKey: "portrait")
        aCoder.encode(userId, forKey: "userId")
        aCoder.encode(reviews, forKey: "reviews")
        aCoder.encode(trustNum, forKey: "trustNum")
        aCoder.encode(expirationDate, forKey: "expirationDate")
        aCoder.encode(account, forKey: "account")
    }
    
}
